<?php
/**
 * insert row to table prestasipelanggaran
 * @param $key
 * @return result
 */
function perpel_select($key=false, $orderBy=false, $groupBy=false){
	$sql = "select * from prestasipelanggaran";
	if ($key){
		$sql .= " where 
				ACHIEV_ID LIKE '%$key%' OR
				ACHIEV_JENIS LIKE '%$key%' OR 
				ACHIEV_BIDANG LIKE '%$key%'";
	}
	if ($groupBy) $sql .= " GROUP BY $groupBy ";
	if ($orderBy) $sql .= " ORDER BY $orderBy ";
	$result = mysql_query($sql);
	return !$result? mysql_error() : $result;
}

function perpel_select2($key=false, $where=false, $orderby=false, $groupBy=false){
	$sql = "select * from prestasipelanggaran";
	if ($key){
		$sql .= " where 
				ACHIEV_ID LIKE '%$key%' OR
				ACHIEV_JENIS LIKE '%$key%' OR 
				ACHIEV_BIDANG LIKE '%$key%' ";
	}
	if ($where) $sql .= ($key? " AND $where " : " where $where ");
	if ($groupBy) $sql .= " GROUP BY $groupBy ";
	if ($orderby) $sql .= " ORDER BY $orderby ";
	$result = mysql_query($sql);
	return !$result? mysql_error() : $result;
}

function perpel_generate_id(){
//	$sql = "SELECT max(substring(`REWARD_ID`,2)) as max FROM `rewarddanpunishment` WHERE 1";
//	$result = mysql_query($sql);
//	$result = mysql_fetch_assoc($result);
//	return 'R'.($result['max']+1);
	return false;
}

/**
 * load row from table prestasipelanggaran
 * @param $id
 * @return result
 */
function perpel_load($id){
	$sql = "select * from prestasipelanggaran
			where ACHIEV_ID = '$id'";
	return mysql_query($sql);
}

/**
 * check wheter the specific id is exist on the table prestasipelanggaran
 * @param $id specific id
 * @return boolean value (true:exist, false:otherwise)
 */
function perpel_isExistID($id){
	$sql = "select * from prestasipelanggaran
			where ACHIEV_ID = '$id'";
	
	$result = mysql_query($sql);
	$result = mysql_fetch_assoc($result);
	if ($res['ACHIEV_ID_ID'] === $id) return true;
	return false;
}

/**
 * insert a record to prestasipelanggaran table
 * @param $id
 * @param $nama
 * @param $jenis
 * @return resu;t
 */
function perpel_add($id, $jenis, $bidang){
	$sql = "insert into prestasipelanggaran ".
			"values ('$id','$jenis','$bidang')";
	return mysql_query($sql);
}

/**
 * update a row in prestasipelanggaran table
 * @param $id
 * @param $nama
 * @param $jenis
 * @return result
 */
function perpel_update($id, $jenis, $bidang){
	$sql = "update prestasipelanggaran ".
			"set ACHIEV_JENIS = '$jenis', 
				ACHIEV_BIDANG = '$bidang'
			where ACHIEV_ID ='$id'";
	return mysql_query($sql);
}

/**
 * delete a row in prestasipelanggaran table
 * @param $id
 * @return boolean
 */
function perpel_delete($id){
	$sql = "delete from prestasipelanggaran ".
			"where ACHIEV_ID='$id'";
	return mysql_query($sql);
}